package com.um.jdy.platform.station.manager;

import cn.hutool.core.bean.BeanUtil;
import com.um.jdy.common.device.entity.po.*;
import com.um.jdy.common.device.entity.vo.FeeEBoxPowerVO;
import com.um.jdy.common.device.entity.vo.FeeEBoxPriceVO;
import com.um.jdy.common.device.entity.vo.FeePlaceTimeVO;
import com.um.jdy.common.device.entity.vo.FeePlaceVO;
import com.um.jdy.common.device.service.*;
import com.um.jdy.platform.station.entity.dto.FeeEBoxDTO;
import com.um.jdy.platform.station.entity.vo.FeeEBoxVO;
import com.um.jdy.rpc.enums.DeviceTypeEnum;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
public class FeeEBoxManager extends BaseManager<FeeEBoxPriceService> {

    @Autowired
    private FeeService feeService;

    @Autowired
    private FeeEBoxPowerService feeEBoxPowerService;

    @Autowired
    private FeePlaceTimeService feePlaceTimeService;

    @Autowired
    private FeePlaceService feePlaceService;

    public FeeEBoxVO get(String id) {
        Fee fee = feeService.getEntity("device_fee_id", id);

        List<FeeEBoxPrice> priceList = service.getList(new String[]{"device_fee_id"}, new String[]{id},
                new String[]{"price"}, new String[]{"asc"});
        List<FeeEBoxPriceVO> prices = UMBeanUtils.copyBeanList(priceList,FeeEBoxPriceVO.class);

        List<FeeEBoxPower> powerList = feeEBoxPowerService.getList(new String[]{"device_fee_id"}, new String[]{id},
                new String[]{"start_power"}, new String[]{"asc"});
        List<FeeEBoxPowerVO> powers = UMBeanUtils.copyBeanList(powerList,FeeEBoxPowerVO.class);


        FeePlace feePlace = feePlaceService.getEntity("device_fee_id",id);
        FeePlaceVO feePlaceVO = UMBeanUtils.copyBean(feePlace,FeePlaceVO.class);

        FeeEBoxVO feePricePowerVO = new FeeEBoxVO();
        feePricePowerVO.setName(fee.getName());
        feePricePowerVO.setRemark(fee.getRemark());
        feePricePowerVO.setPrices(prices);
        feePricePowerVO.setPowers(powers);
        feePricePowerVO.setPlace(feePlaceVO);
        return feePricePowerVO;
    }

    @Transactional
    public FeeEBoxVO add(FeeEBoxDTO feeEbDTO) {
        Fee fee = UMBeanUtils.copyBean(feeEbDTO,Fee.class);
        fee.setCreateTime(new Date());
        fee.setDeviceType(DeviceTypeEnum.EBox.getKey());
        feeService.addEntity(fee);

        FeePlace feePlace = UMBeanUtils.copyBean(feeEbDTO.getPlace(),FeePlace.class);
        feePlace.setDeviceFeeId(fee.getDeviceFeeId());
        feePlaceService.add(feePlace);

        //保存新价格列表
        feeEbDTO.getPrices().stream().forEach(priceDTO -> {
            priceDTO.setDeviceFeeId(fee.getDeviceFeeId());
            service.add(BeanUtil.toBean(priceDTO, FeeEBoxPrice.class));
        });

        //保存新功率列表
        feeEbDTO.getPowers().stream().forEach(FeePowerDTO -> {
            FeePowerDTO.setDeviceFeeId(fee.getDeviceFeeId());
            feeEBoxPowerService.add(BeanUtil.toBean(FeePowerDTO, FeeEBoxPower.class));
        });

        return BeanUtil.toBean(feeEbDTO, FeeEBoxVO.class);
    }

    @Transactional
    public FeeEBoxVO update(String id, FeeEBoxDTO feeEbDTO) {
        Fee fee = UMBeanUtils.copyBean(feeEbDTO,Fee.class);
        fee.setDeviceFeeId(id);
        fee.setUpdateTime(new Date());
        feeService.updateEntity(fee);

        feePlaceService.delete("device_fee_id",id);
        FeePlace feePlace = UMBeanUtils.copyBean(feeEbDTO.getPlace(),FeePlace.class);
        feePlace.setDeviceFeeId(id);
        feePlaceService.add(feePlace);


        //删除原价格列表
        service.delete("device_fee_id", id);
        //保存新价格列表
        feeEbDTO.getPrices().stream().forEach(priceDTO -> {
            priceDTO.setDeviceFeeId(id);
            service.save(BeanUtil.toBean(priceDTO, FeeEBoxPrice.class));
        });

        //删除原功率列表
        feeEBoxPowerService.delete("device_fee_id", id);
        //保存新功率列表
        feeEbDTO.getPowers().stream().forEach(FeePowerDTO -> {
            FeePowerDTO.setDeviceFeeId(id);
            feeEBoxPowerService.save(BeanUtil.toBean(FeePowerDTO, FeeEBoxPower.class));
        });


        return BeanUtil.toBean(feeEbDTO, FeeEBoxVO.class);
    }
}
